﻿#ifndef _EVENT_H_
#define _EVENT_H_

#include <thread>
#include <mutex>
#include <map>
#include <functional>
#include <sys/time.h>


enum TypeEvent : unsigned int
{
    TypeRead = 0,
    TypeWrite,
    TypeTimeOut,
    TypeWake,
    TypeError,
};

class Event 
{
public:
    explicit Event(int cycle);
    virtual ~Event();

    int start();
    int stop();

    int WakeUpTask();

    inline int IsStart() { return m_startup; }

    inline int GetCycle() { return m_cycle; }
    inline void RetSetCycle(int cycle = 0) { m_cycle = cycle; }
    

    int ListenEvents(int handler, std::function<void(int, TypeEvent)> func);
    int DeleteEvents(int handler);

    virtual void CycleTask(); /* 超时事件 */

private:

    inline int max(int a, int b) { if (a > b)return a; else return b; }
    void DataProcess();

    int m_cycle;
    int m_startup;
    int m_event;

    std::thread* m_work;
    std::map<int, std::function<void(int, TypeEvent)>>  m_func_table;
};


#endif /* event */